package com.x.erp.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.x.erp.model.CarLog;
import com.x.erp.util.ConstantState;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.CarOrder;
import com.x.erp.rpc.CarOrderRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

import javax.servlet.http.HttpServletRequest;

/**
 * 预订信息
 */
@RestController
public class CarOrderController extends BaseController{

	@Reference
	private CarOrderRpcService carOrderRpcService;//预订信息服务

	/**
	 * 查询车辆信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getCarOrderList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarOrderList(CarOrder carOrder) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carOrderRpcService.selectCarOrderList(carOrder));
	}

	/**
	 * 查询车辆预订数量
	 * @param carOrder
	 * @return
	 */
	@RequestMapping(value = "/getCarOrderNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarOrderNum(CarOrder carOrder) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", carOrderRpcService.selectCarOrderNum(carOrder));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据ID查询预订信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getCarOrderById", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarOrderById(CarOrder carOrder) {
		if(carOrder == null || StringUtils.isEmpty(carOrder.getOrderId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "预订ID不能为空！");
		}
		List<CarOrder> carOrders = carOrderRpcService.selectCarOrderList(carOrder);
		if(carOrders != null && carOrders.size() > 0) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", carOrders.get(0));
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增预订信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertCarOrder", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertCarOrder(CarOrder carOrder, HttpServletRequest request) {
		String source = request.getHeader("source");
		if(carOrder == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "预订信息不能为空！");
		}
		if(StringUtils.isEmpty(carOrder.getCarId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		if(StringUtils.isEmpty(carOrder.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "操作用户Id不能为空！");
		}
		if(carOrder.getOrderSaleType() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "销售类型不能为空！");
		}
		if(carOrder.getOrderDealDate() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "成交日期不能为空！");
		}
		if(carOrder.getOrderPrice() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "成交金额不能为空！");
		}
		if(StringUtils.isEmpty(carOrder.getOrderDealUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "成交员工ID不能为空！");
		}
		//记录日志信息
		CarLog carLog = new CarLog("车辆预订", new Date(), carOrder.getUserId(), carOrder.getCarId());
		carLog.setCarStateKey("carOrderState");
		carLog.setCarStateValue(ConstantState.CAR_ORDER_STATE_ORDERED);
		carLog.setCarLogSource(source);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", carOrderRpcService.insertCarOrder(carOrder, carLog));
	}

	/**
	 * 修改预订信息
	 * @param carOrder
	 * @return
	 */
	@RequestMapping(value = "/updateCarOrder", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateCarOrder(CarOrder carOrder) {
		if(carOrder == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "预订信息不能为空！");
		}
		if(StringUtils.isEmpty(carOrder.getOrderId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "预订ID不能为空！");
		}
		carOrderRpcService.updateCarOrder(carOrder);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

}
